package com.mobile.framework.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import static com.mobile.framework.util.HtmlUtil.*;
public class HtmlUtil {
    public static final String ENCODING = "UTF-8";
    public static String removeHtmlCode(String html) throws SAXException, IOException {
        return removeHtmlCode(html, "UTF-8");
    }
    public static String removeHtmlCode(String html, String code) throws SAXException, IOException {
        // 生成html parser
        DOMParser parser = new DOMParser();
        // 设置网页的默认编码
        parser.setProperty("http://cyberneko.org/html/properties/default-encoding", code);
        // input file
        BufferedReader in = new BufferedReader(new StringReader(html));
        parser.parse(new InputSource(in));
        Document doc = parser.getDocument();
        // 获得body节点，以此为根，计算其文本内容
        Node body = doc.getFirstChild();

        String value = textExtractor(body);
        return ChineseUtil.replaceSpace2(value);
    }
    public static String encodeURIComponent(String s) {
        return encodeURIComponent(s, ENCODING);
    }
    public static void main(String[] arge) throws IOException, SAXException {
        //System.out.println("+".replaceAll("\\+","%20"));
    }
    public static String encodeURIComponent(String s, String encoding) {
        // Encode URL
        try {
            s = URLEncoder.encode(s, encoding);
        } catch (Exception e) {
        }
        s = s.replaceAll("\\+","%20");
        s = s.replaceAll("%2B","+");
        return s;
    }

    public static String decodeURIComponent(String s) {
        return decodeURIComponent(s, ENCODING);
    }

    public static String decodeURIComponent(String s, String encoding) {
        s = s.replaceAll("%u[0-9a-fA-F]{4}", "");
        s = s.replaceAll("\\+","%2B");
        s = s.replaceAll("%20","+");
        try {
            s = URLDecoder.decode(s, encoding);
        } catch (Exception e) {
        }
        return s;
    }
    public static String textExtractor(Node root) {
        // 若是文本节点的话，直接返回
        if (root.getNodeType() == Node.TEXT_NODE) {
            return root.getNodeValue().trim();
        }
        if (root.getNodeType() == Node.ELEMENT_NODE) {
            Element elmt = (Element) root;
            // 抛弃脚本
            if (elmt.getTagName().equals("STYLE")
                    || elmt.getTagName().equals("SCRIPT"))
                return "";

            NodeList children = elmt.getChildNodes();
            StringBuilder text = new StringBuilder();

            for (int i = 0; i < children.getLength(); i++) {
                text.append(textExtractor(children.item(i)));
            }
            return text.toString();
        }
        // 对其它类型的节点，返回空值
        return "";
    }

}
